<?php
/**
* Copyright (c) 2002-2007 BioWare Corp.
* http://www.bigdaddymonitor.org
*
* This file is part of the BigDaddy Monitoring
* program. It is free software distributed under
* the terms of the BSD License. It is ditributed
* in the hopes that it will be useful, but
* WITHOUT ANY WARRANTY; without event the implied
* warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE.
*
* The BSD License is available from:
* http://www.opensource.org/licenses/bsd-license.php
**/

function SetBoolHistory($nLastReport, $nMonLastReport, $nServerID, $nModuleID, $nServerModuleID)
{
    $sql = "INSERT INTO ".MA_TABLE." SET
        lastreport = '".$nLastReport."',
        mon_lastreport = '".$nMonLastReport."',
        serverID = '".$nServerID."',
        serverModuleID = ".$nServerModuleID.",
        moduleID = '".$nModuleID."'";
    db_query($sql, array("dbHandle" => "bigdaddystats"));
}

//Set the wait time out on the original connection as we seem to have some issues
$sql = "SET WAIT_TIMEOUT=28800";
db_query($sql);

//When did I last end?
$sql = "SELECT last_end
    FROM ma_cronstatus
    WHERE ma_type = '".MA_TYPE."'";
$aResult = db_fetch_result(db_query($sql, array("dbHandle" => "bigdaddystats")));

$nLastEnd = $aResult["last_end"];

if ($nLastEnd < 10000)
{
    $nLastEnd = INSTALL_DATE-86400;
}

//Set up the time for now.
$nNow = time();

$nDoTime = $nLastEnd;

$sql = "SELECT sm.ID, sm.serverID, sm.moduleID
        FROM server_module sm
        LEFT JOIN module m ON m.ID = sm.moduleID
        WHERE m.type = 1";
$aModules = db_fetch_array(db_query($sql));

//var_dump($aModules);

if (is_array($aModules))
{
    foreach ($aModules as $aModule)
    {
        
        $sql = "SELECT lastreport, mon_lastreport
            FROM server_module_history
            WHERE serverModuleID = ".$aModule["ID"]."
                AND mon_lastreport BETWEEN $nDoTime AND $nNow
            ORDER BY mon_lastreport ASC";
        $aResult = db_fetch_array(db_query($sql, array("dbHandle" => "bigdaddystats")));
        
        if (is_array($aResult))
        {
            $nCurrentValue = 99;
            $i=0; //Counter for the number of rows we are going over
            
            foreach ($aResult as $aRow)
            {
                if ($nCurrentValue != $aRow["lastreport"] )
                {
                    //We need to enter in a value into the bool history table
                    SetBoolHistory(
                        $aRow["lastreport"],
                        $aRow["mon_lastreport"],
                        $aModule["serverID"],
                        $aModule["moduleID"],
                        $aModule["ID"]
                        );
                    $nCurrentValue = $aRow["lastreport"];
                    $i=0;
                }
                else if ($i%MA_INCREMENT_COUNT == 0 && $i != 0)
                {
                    //We need to enter in a value into the bool history table
                    SetBoolHistory(
                        $aRow["lastreport"],
                        $aRow["mon_lastreport"],
                        $aModule["serverID"],
                        $aModule["moduleID"],
                        $aModule["ID"]
                        );
                    $i++;
                }
                else
                {
                    $i++;
                }
            }
        }
    }
}
?>
